package com.lzq.leetcode.hoot100;

import com.lzq.leetcode.hoot100.ds.ListNode;

import java.util.concurrent.atomic.AtomicBoolean;

public class s2 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        int pre = 0;
        ListNode head = new ListNode();
        ListNode p = head;
        int a, b, sum, cur;
        while (l1 != null || l2 != null) {
            a = l1 == null ? 0 : l1.val;
            b = l2 == null ? 0 : l2.val;

            sum = a + b + pre;
            cur = sum % 10;
            pre = sum / 10;

            p.next = new ListNode(cur);

            l1 = l1 == null ? l1 : l1.next;
            l2 = l2 == null ? l2 : l2.next;
            p = p.next;
        }

        if (pre != 0) {
            p.next = new ListNode(pre);
        }

        return head.next;
    }

    public static void main(String[] args) {
        System.out.println(new s2().addTwoNumbers(new ListNode(new int[]{2, 4, 3}), new ListNode(new int[]{5, 6, 4})));
    }
}
